package com.copera.cn.kaka.util;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.copera.cn.kaka.vo.ExchangeRuleVo;
import com.copera.cn.kaka.vo.ImageVO;
import com.copera.cn.kaka.vo.PointPraiseSetVo;
import com.copera.cn.kaka.vo.ShopVo;

public class JsonUtil {

	public static String getMessage(String json) throws JSONException {
		if (json == null) {
			return null;
		}
		JSONObject jo = new JSONObject(json);
		String message = jo.getString("message");
		if (message == null || "null".equals(message)) {
			message = null;
		}
		return message;
	}

	public static ShopVo toShop(String json) throws JSONException{
		if (json == null) {
			return null;
		}
		ShopVo shop = new ShopVo();
		JSONObject jo = new JSONObject(json);
		JSONObject u = jo.getJSONObject("shop");
		shop.setId(u.getString("id"));
		shop.setAddress(u.getString("address"));
		shop.setPhone(u.getString("loginName"));
		shop.setPhone1(u.getString("phone"));
		shop.setShopName(u.getString("shopName"));
		shop.setTrackToday(jo.getString("trackToday"));
		shop.setHeadImage(u.getString("headImage"));
		shop.setExchangeToday(jo.getString("exchangeToday"));
		shop.setPraiseCount(u.getString("total_count"));
		shop.setShareCount(u.getString("total_count1"));
		shop.setValidDate(u.getString("validDate"));
		shop.setShopId(jo.getString("shopId"));
		
		JSONArray jsonArr = jo.getJSONArray("ruleList");
		if (jsonArr != null && jsonArr.length() > 0) {
			List<ExchangeRuleVo> list = new ArrayList<ExchangeRuleVo>();
			ExchangeRuleVo exchangeRuleVo;
			JSONObject object;
			for (int i = 0; i < jsonArr.length(); i++) {
				exchangeRuleVo = new ExchangeRuleVo();
				object = jsonArr.getJSONObject(i);
				exchangeRuleVo.setId(object.getString("id"));
				exchangeRuleVo.setExchangeContent(object.getString("exchangeContent"));
				exchangeRuleVo.setPointCount(object.getString("pointCount"));
				list.add(exchangeRuleVo);
			}
			shop.setRuleList(list);
		}
		return shop;
	}
	public static PointPraiseSetVo toPointPraiseSetVo(JSONObject jo) throws JSONException{
		
		PointPraiseSetVo vo = new PointPraiseSetVo();
		JSONObject  po = jo.getJSONObject("pointPraiseSet");
		vo.setDayCount(po.getInt("dayCount")+"");
		vo.setEnabled(po.getString("enabled"));
		vo.setPresenterCount(po.getInt("presenterCount")+"");
		vo.setTotalCount(po.getInt("totalCount")+"");
		return vo;
	}
    public static PointPraiseSetVo toShareSetVo(JSONObject jo) throws JSONException{
		
		PointPraiseSetVo vo = new PointPraiseSetVo();
		JSONObject  po = jo.getJSONObject("shareSet");
		vo.setDayCount(po.getInt("dayCount")+"");
		vo.setEnabled(po.getString("enabled"));
		vo.setPresenterCount(po.getInt("presenterCount")+"");
		vo.setTotalCount(po.getInt("totalCount")+"");
		return vo;
	}
   public static List<ImageVO> toShopImageVo(JSONArray jsonArr) throws JSONException{
	   List<ImageVO> images = new ArrayList<ImageVO>();
	   if (jsonArr != null && jsonArr.length() > 0) {
			for (int i = 0; i < jsonArr.length(); i++) {
				ImageVO vo = new ImageVO();
				JSONObject jo = jsonArr.getJSONObject(i);
				vo.setFilepath(jo.getString("fileName"));
				vo.setType(jo.getString("type"));
				vo.setId(jo.getString("id"));
				vo.setName(jo.getString("name"));
				vo.setTitle(jo.getString("description"));
				images.add(vo);
			}
	   }
	   return images;
	}

}
